package com.hongning.railway.ui.main.mine.experience;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentExperiencepuboreditBinding;
import com.hongning.railway.entity.resbean.ResExperienceBean;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 发表
 */
public class ExperiencePubOrEditFragment extends BaseFragment<FragmentExperiencepuboreditBinding,ExperiencePubOrEditFragmentViewModel> {
    ResExperienceBean.SelectResultBean data;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_experiencepuboredit;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        if (getArguments()!=null){
            data=getArguments().getParcelable(Constant.EXTRA_DATA);
        }

    }

    @Override
    public void initData() {
        super.initData();
        binding.appTitle.tvRight.setVisibility(View.VISIBLE);
        if (data!=null){
            binding.appTitle.tvRight.setText("修改");
            swithEnable(false);
            viewModel.setData(data);
        }else {
            binding.appTitle.tvRight.setText("发表");
        }
        binding.appTitle.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ((TextView)v).getText().toString();
                switch (text){
                    case "修改":
                        swithEnable(true);
                        binding.appTitle.tvRight.setText("保存");
                        break;
                    case "保存":
                        viewModel.update(data.getId());
                        break;
                    case "发表":
                        viewModel.pub();
                        break;
                }
            }
        });
        binding.appTitle.tvTopTitle.setText("心得体会");
        binding.appTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.appTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public ExperiencePubOrEditFragmentViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(ExperiencePubOrEditFragmentViewModel.class);
    }

    public void swithEnable(boolean isenable){
        binding.etTitle.setEnabled(isenable);
        binding.etContent.setEnabled(isenable);
        if (isenable){
            binding.etTitle.requestFocus();
            binding.etTitle.setSelection(binding.etTitle.getText().length());
        }
    }
}
